package com.ftg.learn.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ftg.learn.course.dto.CategoryDto;
import com.ftg.learn.course.entity.Category;
import com.ftg.learn.course.entity.Course;
import com.ftg.learn.course.mapper.CategoryMapper;
import com.ftg.learn.course.mapper.CourseMapper;
import com.ftg.learn.course.result.Result;
import com.ftg.learn.course.service.CategoryService;
import com.ftg.learn.course.service.CourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public Result<List<Category>> getAll() {
        List<Category> categories = list();
        if(categories== null){
            return Result.build(500,"获取失败");
        }
        return Result.build(200,"获取成功",categories);
    }

    @Override
    public Result<String> add(CategoryDto categoryDto) {
        LambdaQueryWrapper<Category> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Category::getName, categoryDto.getName());
        if(getOne(queryWrapper)!=null){
            return Result.build(500,"添加失败，该分类已存在");
        }
        Category category= BeanUtil.copyProperties(categoryDto,Category.class);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        boolean save = save(category);
        if(!save){
            return Result.build(500,"添加失败");
        }
        return Result.build(200,"添加成功");
    }

    @Override
    public Result<String> MyUpdate(Category category) {
        //TODO name重名问题未解决
        LambdaUpdateWrapper<Category> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper
                .set(Category::getName, category.getName())
                .set(Category::getType, category.getType())
                .set(Category::getSort, category.getSort())
                .eq(Category::getId, category.getId());
        category.setUpdateTime(LocalDateTime.now());
        boolean update = updateById(category);
        if(!update){
            return Result.build(500,"修改失败");
        }
        return Result.build(200,"修改成功");
    }

    @Override
    public Result<String> MyDelete(Long id) {
        LambdaQueryWrapper<Course> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper
                .eq(Course::getCategoryId, id);
        if(courseMapper.selectList(queryWrapper).size()!=0){
            return Result.build(500,"删除失败，该分类下有课程");
        }
        if(!removeById(id)){
            return Result.build(500,"删除失败");
        }
        return Result.build(200,"删除成功");
    }

    @Override
    public Result<Category> MyGetOne(Long id) {
        Category category = getById(id);
        if(category==null){
            return Result.build(500,"获取失败");
        }
        return Result.build(200,"获取成功",category);
    }
}
